package com.qf.service.impl;

import com.qf.dao.OrderderDao;
import com.qf.dao.impl.OrderderDaoImpl;
import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.OrederService;
import com.qf.util.DataSourceUtil;

import javax.swing.plaf.nimbus.State;
import java.util.List;

public class OrederServiceImpl implements OrederService {
    private OrderderDao orderderDao = new OrderderDaoImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
            try {
                //事务处理
                //1·开启事务
                DataSourceUtil.begin();
                //1·1向订单中添加一条记录
                orderderDao.add(order);
                //1·2向订单详情中添加n条记录
                for(Cart cart: cartList){
                    OrderDetail orderDetail = new OrderDetail(0,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                    orderderDao.addDetail(orderDetail);
                }
                //1·3删除购物车中的数据
                CartService cartService = new CartserviceImpl();
                cartService.clear(order.getUid());
                //2·提交事务
                DataSourceUtil.commit();
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    //3·事务回滚
                    DataSourceUtil.rollback();
                } catch (Exception e1) {
                    e1.printStackTrace();
                    throw new RuntimeException(e1);
                }
            } finally {
                try {
                    //4·事务释放资源
                    DataSourceUtil.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }

    @Override
    public void updateState(String oid, int state) {
        orderderDao.updateState(oid, state);
    }

    @Override
    public List<Order> find(Integer uid) {
        List<Order> orderList = orderderDao.select(uid);
        if (orderList != null && orderList.size()>0) {
            //获取地址
            AddressService addressService = new AddressServiceImpl();
            List<Address> addList = addressService.find(uid);
            if (addList != null) {
                for (Order order : orderList) {
                    for (Address address : addList) {
                        if(order.getAid()==address.getId()){
                            order.setAddress(address);
                        }
                    }
                }
            }
        }
        return orderList;
    }

    @Override
    public Order findByOid(String oid) {
        Order order = orderderDao.selectByOid(oid);
        if (order != null) {
            AddressService addressService = new AddressServiceImpl();
            List<Address> addList = addressService.find(order.getUid());
            if (addList != null) {
                for (Address address : addList) {
                    if(address.getId()==order.getAid()){
                        order.setAddress(address);
                    }
                }
            }
        }
        return order;
    }

    @Override
    public List<OrderDetail> findDetail(String oid) {
        List<OrderDetail>orderDetails = orderderDao.selectDetail(oid);
        if(orderDetails!=null&&orderDetails.size()>0){
            GoodsService goodsService = new GoodsServiceImpl();
            for (OrderDetail orderDetail : orderDetails) {
                Goods goods = goodsService.findById(orderDetail.getPid());
                orderDetail.setGoods(goods);
            }
        }
        return orderDetails;
    }
}
